<?php
namespace app\manage\model;
use think\Db;

class Analysis extends Base
{
    //非自然月，留存率
    public function keepPrice($plat, $startTime, $endTime, $gameId, $type){
        $year = date('Y', $startTime);

        //游戏条件
        $whereGameId = '';
        if($gameId){
            $whereGameId .= " AND game_id={$gameId}";
        }

        //平台类型
        if($plat=='cps'){
            $whereChannelId = " && channel_id='1000'";
        }else if($plat=='sdk'){
            $whereChannelId = " && channel_id>='5000'";
        }else{
            $whereChannelId = '';
        }

        $whereAdd = "bond_time between {$startTime} AND {$endTime}" . $whereGameId . $whereChannelId; //新增条件

        $selectAdd = Db::name('GameUserY_'.$year)
            ->field("my_user_id, bond_time")
            ->where($whereAdd)
            ->select();

        if(!$selectAdd){
            return [];
        }

        $arrayAdd = [];
        foreach($selectAdd as $k=>$v){
            $arrayAdd[date('Y-m-d', $v['bond_time'])][$v['my_user_id']] = 1;
        }

        $gameIdName = model('Game')->gameIdName();
        $selectGame = model('ViewGameManage')->field('game_id, online_time_region')->order('game_id desc')->select();

        $gameIdOnline = [];
        foreach ($selectGame as $k=>$v){
            $arr = explode('-', $v['online_time_region']);
            if(isset($arr[0])){
                if($arr[0]=='' || $arr[0]==0){
                    $gameIdOnline[$v['game_id']] = '';
                }else{
                    $gameIdOnline[$v['game_id']] =  date('Y-m-d', $arr[0]);
                }
            }else{
                $gameIdOnline[$v['game_id']] = '';
            }
        }

        $allData = [];
        if($type=='keep'){
            $i=0;
            for ($start = $startTime; $start <= $endTime; $start += 24 * 3600) {
                $dateTime = date('Y-m-d', $start);
                $dataAdd = isset($arrayAdd[$dateTime]) ? $arrayAdd[$dateTime] : [];
                $add = count($dataAdd);
                $allData[$i]['date'] = $dateTime;
                $allData[$i]['game_id'] = $gameId;
                $allData[$i]['game_name'] = isset($gameIdName[ $gameId ]) ? $gameIdName[ $gameId ] : '';
                $allData[$i]['online_time'] = isset($gameIdOnline[ $gameId ]) ? $gameIdOnline[ $gameId ] : '';
                $allData[$i]['add'] = $add;
                $allData[$i]['keep1'] =  time()-$start>86400*2 ? $this->arrayActive($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 1) : '';
                $allData[$i]['keep7'] = time()-$start>86400*7 ? $this->arrayActive($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 6) : '';
                $allData[$i]['keep30'] = time()-$start>86400*30 ? $this->arrayActive($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 29) : '';
                $allData[$i]['keep60'] = time()-$start>86400*60 ? $this->arrayActive($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 59) : '';
                $allData[$i]['keep90'] = time()-$start>86400*90 ? $this->arrayActive($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 89) : '';
                $i++;
            }
        }else if($type=='price'){
            $i=0;
            for ($start = $startTime; $start <= $endTime; $start += 24 * 3600) {
                $dateTime = date('Y-m-d', $start);
                $dataAdd = isset($arrayAdd[$dateTime]) ? $arrayAdd[$dateTime] : [];
                $add = count($dataAdd);
                $allData[$i]['date'] = $dateTime;
                $allData[$i]['game_id'] = $gameId;
                $allData[$i]['game_name'] = isset($gameIdName[ $gameId ]) ? $gameIdName[ $gameId ] : '';
                $allData[$i]['online_time'] = isset($gameIdOnline[ $gameId ]) ? $gameIdOnline[ $gameId ] : '';
                $allData[$i]['add'] = $add;
                $allData[$i]['price3'] =  time()-$start>86400*3 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 3) : '';
                $allData[$i]['price7'] = time()-$start>86400*7 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 7) : '';
                $allData[$i]['price14'] = time()-$start>86400*14 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 14) : '';
                $allData[$i]['price30'] = time()-$start>86400*30 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 30) : '';
                $allData[$i]['price45'] = time()-$start>86400*45 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 45) : '';
                $allData[$i]['price60'] = time()-$start>86400*60 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 60) : '';
                $allData[$i]['price90'] = time()-$start>86400*90 ? $this->arrayPrice($dataAdd, $start, $start+86399, $whereGameId, $whereChannelId, 90) : '';
                $i++;
            }
        }
        return $allData;
    }

    private function arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, $num, $type='index'){
        $month = date('Ym', $startTime+(86400*$num));
        $startTime1 = $startTime+(86400*$num);
        $endTime1 = $endTime+(86400*$num);

        $whereActive = "in_time between {$startTime1} AND {$endTime1}" . $whereGameId . $whereChannelId; //活跃条件

        if($month > date('Ym') || !count($arrayAdd)){
            return 0;
        }

        $selectActive = Db::name('GameUserLogM_'.$month)
	        ->distinct(true)
            ->where($whereActive)
            ->column('my_user_id');

	    $count = 0;
        foreach ($selectActive as $k=>$v){
            if(isset($arrayAdd[$v])){
                $count += 1;
            }
        }
        if($type=='index'){
            return $count . '（' . round($count/count($arrayAdd)*100, 2) . '%）';
        }else{
            return $count;
        }
    }

    private function arrayPrice($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, $num){
        $startTime1 = $startTime;
        $endTime1 = $endTime+(86400*($num-1));
        $year = date('Y', $startTime1);

        $wherePrice = "pay_time between {$startTime1} AND {$endTime1} AND debug=0 AND pay_result=1" . $whereGameId . $whereChannelId; //活跃条件

        $y1 = date('Y', $startTime1);
        $y2 = date('Y', $endTime1);
        if($y2>date('Y') || !count($arrayAdd)){
            return 0;
        }

        if($y1==$y2){
            $selectPrice = Db::name('PayOrderY_'.$year)
                ->field("my_user_id, sum(total_price) as price")
                ->where($wherePrice)
                ->group('my_user_id')
                ->select();
        }else{
            $field = "my_user_id, total_price";
            $subQuery = Db::name('PayOrderY_'.$y1)
                ->field($field)
                ->where($wherePrice)
                ->union("select {$field} from sdk_pay_order_y_{$y2} where ({$wherePrice})", true)
                ->buildSql();

            $selectPrice = Db::table($subQuery.' y')
                ->field('my_user_id, sum(total_price) as price')
                ->group('my_user_id')
                ->select();
        }

        $count = 0;
        foreach ($selectPrice as $k=>$v){
            if(isset($arrayAdd[$v['my_user_id']])){
                $count += $v['price'];
            }
        }

        return round($count/100/(count($arrayAdd)), 2);
    }

    public function oneKeepPrice($plat, $startTime, $endTime, $gameId, $type){
        $year = date('Y', $startTime);

        //游戏条件
        $whereGameId = '';
        if($gameId){
            $whereGameId .= " AND game_id={$gameId}";
        }

        //平台类型
        if($plat=='cps'){
            $whereChannelId = " && channel_id='1000'";
        }else if($plat=='sdk'){
            $whereChannelId = " && channel_id>='5000'";
        }else{
            $whereChannelId = '';
        }

        $whereAdd = "bond_time between {$startTime} AND {$endTime}" . $whereGameId . $whereChannelId; //新增条件

        $selectAdd = Db::name('GameUserY_'.$year)
            ->where($whereAdd)
            ->column('my_user_id');

        if(!$selectAdd){
            return [];
        }

        $arrayAdd = [];
        foreach($selectAdd as $k=>$v){
            $arrayAdd[$v] = 1;
        }

        $data = [];
        $allData = [];
        if($type=='keep'){
            //计算留存
            $data[] = count($arrayAdd);
            $data[] = time()-$startTime>86400*2 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 1, 'chart') : 0;
            $data[] = time()-$startTime>86400*3 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 2, 'chart') : 0;
            $data[] = time()-$startTime>86400*4 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 3, 'chart') : 0;
            $data[] = time()-$startTime>86400*5 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 4, 'chart') : 0;
            $data[] = time()-$startTime>86400*6 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 5, 'chart') : 0;
            $data[] = time()-$startTime>86400*7 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 6, 'chart') : 0;
            $data[] = time()-$startTime>86400*14 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 13, 'chart') : 0;
            $data[] = time()-$startTime>86400*30? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 29, 'chart') : 0;
            $data[] = time()-$startTime>86400*60? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 59, 'chart') : 0;
            $data[] = time()-$startTime>86400*90 ? $this->arrayActive($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 89, 'chart') : 0;

            $data2 = [];
            foreach ($data as $k=>$v){
                $data2[] = 100-$v;
            }

            $allData[0]['name'] = '留存';
            $allData[0]['data'] = $data;
        }else if($type=='price'){
            //计算充值金额
            $price7 = $this->arrayPrice($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 7);
            $price14 = $this->arrayPrice($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 14);
            $price30 = $this->arrayPrice($arrayAdd, $startTime, $endTime, $whereGameId, $whereChannelId, 30);

            foreach ($price7 as $k=>$v){
                $data[$k]['add'] = $v['count'];
                $data[$k]['price7'] = isset($v['price'])&&$v['price']!=0 ? round($v['price']/100/$v['count'],2) : '0';
            }
            foreach ($price14 as $k=>$v){
                $data[$k]['add'] = $v['count'];
                $data[$k]['price14'] = isset($v['price'])&&$v['price']!=0 ? round($v['price']/100/$v['count'],2) : '0';
            }
            foreach ($price30 as $k=>$v){
                $data[$k]['add'] = $v['count'];
                $data[$k]['price30'] = isset($v['price'])&&$v['price']!=0 ? round($v['price']/100/$v['count'],2) : '0';
            }
        }

        return json_encode($allData);
    }

}
